package com.object3D
{
	import away3d.Away3D;
	import away3d.containers.ObjectContainer3D;
	import away3d.entities.Mesh;
	import away3d.materials.ColorMaterial;
	import away3d.materials.TextureMaterial;
	import away3d.materials.methods.EffectMethodBase;
	import away3d.materials.methods.FogMethod;
	import away3d.materials.methods.PlanarReflectionMethod;
	import away3d.primitives.PlaneGeometry;
	import away3d.textures.PlanarReflectionTexture;
	import away3d.utils.Cast;
	
	import com.engine.AwayEngine;
	import com.engine.PowerEngine;
	import com.manager.CarMaterial;
	import com.utils.PowerLoader;
	import com.vo.CarVo;
	import com.vo.PartVo;
	
	import flash.display.Bitmap;
	
	public class BaseCar
	{
		protected static const FIX_TARGET_Y:int = -10;
		
		protected var car3D:ObjectContainer3D;//3D视图容器
		protected var carVo:CarVo;
		
		protected var interiorMaterial:TextureMaterial;
		protected var shadowMaterial:TextureMaterial;
		
		protected var shadow:Mesh;
		protected var wheelArray:Vector.<NewBaseWheel> = new Vector.<NewBaseWheel>();
		
		public function show(carVo:CarVo,x:Number = 0,y:Number = 0,z:Number = 0):void{
			this.carVo = carVo;
			initFace();
			car3D.x = x;
			car3D.y = y;
			car3D.z = z;
		}
		
		public function hide():void{
			if(car3D != null)AwayEngine.removeMesh(car3D);
		}
		
		public function replaceWheel(pvo:PartVo):void
		{
			var length:int = wheelArray.length - 1;
			if(length < 0) return;
			for(var i:int = length;i >= 0;i--)
			{
				(wheelArray[i]).replaceWheel(pvo);
			}
		}
		
		protected function open():void{
			var wheel:BaseWheel;
			wheel = new BaseWheel();
			wheel.show(car3D,carVo.leftForewheel.x,FIX_TARGET_Y + carVo.leftForewheel.y, carVo.leftForewheel.z);
			wheelArray.push(wheel);
			wheel = new BaseWheel();
			wheel.show(car3D,carVo.rightForewheel.x,FIX_TARGET_Y + carVo.rightForewheel.y, carVo.rightForewheel.z,true);
			wheelArray.push(wheel);
			wheel = new BaseWheel();
			wheel.show(car3D,carVo.leftRearWheel.x,FIX_TARGET_Y + carVo.leftRearWheel.y, carVo.leftRearWheel.z);
			wheelArray.push(wheel);
			wheel = new BaseWheel();
			wheel.show(car3D,carVo.rightRearWheel.x,FIX_TARGET_Y + carVo.rightRearWheel.y, carVo.rightRearWheel.z,true);
			wheelArray.push(wheel);
		}
		
		protected function initFace():void
		{
			if(car3D == null){
				car3D = new ObjectContainer3D();
				car3D.scale(.8);
				if(shadow == null)open();
				init();
			}
			AwayEngine.addMesh(car3D);
		}
		
		protected function init():void
		{
			initMaterial();
			loadPNG();
			loadModel();
		}
		
		protected function initMaterial():void
		{
			interiorMaterial = new TextureMaterial();
			shadowMaterial = new TextureMaterial();
			
			if(PowerEngine.platform != PowerEngine.PLAT_PHONE){
				shadowMaterial.alpha = .5;
				shadow = new Mesh(new PlaneGeometry(1120, 1120), shadowMaterial);
				car3D.addChild(shadow);
				shadow.y = FIX_TARGET_Y + 3;
			}
		}
		/**
		 * 加载所有贴图文件
		 */		
		protected function loadPNG():void
		{
			PowerLoader.loadFile(carVo.interiorMap,loadMap,interiorMaterial);
			PowerLoader.loadFile(carVo.shadowMap,loadMap,shadowMaterial);
		}		
		
		protected function loadMap(bmp:Bitmap,material:TextureMaterial):void{
			material.texture = Cast.bitmapTexture(bmp);
		}
		
		protected function loadModel():void
		{
			PowerLoader.loadFile(carVo.bodyPath,loadMesh,'body');
			PowerLoader.loadFile(carVo.glassPath,loadMesh,'glass');
			PowerLoader.loadFile(carVo.bumperPath,loadMesh,'bumper');
			PowerLoader.loadFile(carVo.interiorPath,loadMesh,'interior');
		}		
		
		protected function loadMesh(mesh:Mesh,name:Object=null):void{
			mesh.y += FIX_TARGET_Y;
//			mesh.z -= 200;
			car3D.addChild(mesh);
			var subMesh:Mesh = mesh.getChildAt(0) as Mesh;
			if((name as String) == 'body'){
				subMesh.material = CarMaterial.bodyMaterial;
			}else if((name as String) == 'glass'){
				subMesh.material = CarMaterial.glassMaterial;
			}else if((name as String)  == 'bumper'){
				subMesh.material = CarMaterial.bumperMaterial;
			}else if((name as String) == 'interior'){
				subMesh.material = interiorMaterial;
			}
		}
		
		
	}
}